/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.server.provider.impl;

import java.util.List;

import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.web.gwt.basics.client.oracle.SimpleSuggestion;
import com.genia.toolbox.web.gwt.form.client.form.Form;
import com.genia.toolbox.web.gwt.form.client.value.DataIdentifier;
import com.genia.toolbox.web.gwt.form.client.value.FormSimpleValue;
import com.genia.toolbox.web.gwt.form.client.value.FormValues;
import com.genia.toolbox.web.gwt.form.client.value.impl.DataIdentifierImpl;
import com.genia.toolbox.web.gwt.form.client.value.impl.FormSimpleValueImpl;
import com.genia.toolbox.web.gwt.form.client.value.impl.FormValuesImpl;
import com.genia.toolbox.web.gwt.form.server.provider.FormProvider;
import com.genia.toolbox.web.gwt.form.server.visitor.impl.InitializeValuesFormVisitor;

/**
 * An implementation of <code>FormProvider</code>. A createForm method is
 * added and is called by getForm. createForm is used to retrieve a form
 * registered in spring.
 */
public abstract class AbstractEditionForm
    implements FormProvider
{

  /**
   * get the display string for a given value. This default implementation
   * always returns null.
   * 
   * @param fieldName
   *          the field name
   * @param value
   *          the current value of the field
   * @return the string to display for the given value
   * @throws BundledException
   *           when an error occurred
   * @see com.genia.toolbox.web.gwt.form.server.provider.FormProvider#getDisplayString(java.lang.String,
   *      java.lang.String)
   */
  public String getDisplayString(String fieldName, String value)
      throws BundledException
  {
    return null;
  }



  /**
   * get suggestions for a field. This default implementation always returns
   * null.
   * 
   * @param dataIdentifier
   *          the data identifier
   * @param fieldName
   *          the field name
   * @param query
   *          the current value of the field
   * @param limit
   *          the maximum number of response
   * @return the list of suggestion for the field
   * @throws BundledException
   *           when an error occurred
   * @see com.genia.toolbox.web.gwt.form.server.provider.FormProvider#getSuggestions(com.genia.toolbox.web.gwt.form.client.value.DataIdentifier,
   *      java.lang.String, java.lang.String, int)
   */
  public List<SimpleSuggestion> getSuggestions(DataIdentifier dataIdentifier, String fieldName, String query, int limit)
      throws BundledException
  {
    return null;
  }

  /**
   * the form identifier.
   */
  private String formIdentifier;



  /**
   * get the {@link Form}.
   * 
   * @param dataIdentifier
   *          the data identifier
   * @return the {@link Form}.
   * @throws BundledException
   *           if problem occures.
   */
  public Form getForm(DataIdentifier dataIdentifier)
      throws BundledException
  {
    return createForm();
  }



  /**
   * factory method for the form to send to the application.
   * 
   * @return the form to send to the application
   */
  public Form createForm()
  {
    return null;
  }



  /**
   * get the string form identifier.
   * 
   * @return the string form identifier.
   */
  public String getFormIdentifier()
  {
    return this.formIdentifier;
  }



  /**
   * setter for the formIdentifier property.
   * 
   * @param formIdentifier
   *          the formIdentifier to set
   */
  public void setFormIdentifier(String formIdentifier)
  {
    this.formIdentifier = formIdentifier;
  }



  /**
   * set the value of the
   * {@link com.genia.toolbox.web.gwt.form.client.value.FormSimpleValue} with
   * the given name in the given {@link FormValues} with the given value.
   * 
   * @param formValues
   *          the {@link FormValues} to modify
   * @param formSimpleValueName
   *          the name of the
   *          {@link com.genia.toolbox.web.gwt.form.client.value.FormSimpleValue}
   *          to modify
   * @param value
   *          the new value
   */
  protected void setValue(FormValues formValues, String formSimpleValueName, Object value)
  {
    FormSimpleValue formSimpleValue = formValues.getFormSimpleValue(formSimpleValueName);
    if (formSimpleValue == null) {
      formSimpleValue = new FormSimpleValueImpl();
      ((FormSimpleValueImpl) formSimpleValue).setName(formSimpleValueName);
      formValues.addFormSimpleValue(formSimpleValue);
    }
    formSimpleValue.setValue(value == null ? null : String.valueOf(value));
  }



  /**
   * set the values of the
   * {@link com.genia.toolbox.web.gwt.form.client.value.FormSimpleValue} with
   * the given name in the given {@link FormValues} with the given values.
   * 
   * @param formValues
   *          the {@link FormValues} to modify
   * @param formSimpleValueName
   *          the name of the
   *          {@link com.genia.toolbox.web.gwt.form.client.value.FormSimpleValue}
   *          to modify
   * @param values
   *          the new values
   */
  protected void setValues(FormValues formValues, String formSimpleValueName, List<String> values)
  {
    FormSimpleValue formSimpleValue = formValues.getFormSimpleValue(formSimpleValueName);
    if (formSimpleValue == null) {
      formSimpleValue = new FormSimpleValueImpl();
      ((FormSimpleValueImpl) formSimpleValue).setName(formSimpleValueName);
      formValues.addFormSimpleValue(formSimpleValue);
    }
    formSimpleValue.setValues(values);
  }



  /**
   * returns the default values for the form.
   * 
   * @return the default values for the form
   * @throws BundledException
   *           when an error occured
   */
  protected FormValues getDefaultValues()
      throws BundledException
  {
    FormValuesImpl defaultValues = new FormValuesImpl();
    defaultValues.setFormIdentifier(getFormIdentifier());
    getForm(null).accept(new InitializeValuesFormVisitor(defaultValues));
    defaultValues.setDataIdentifier(new DataIdentifierImpl());
    return defaultValues;
  }
}
